<? require ("comun/ini.php"); require ("comun/header.php"); ?>
<br />
<script>var mygrid,i=0;</script>
<span class="titulo_maestro">Generar Reporte</span>
<div id="formulario">
	<table width="400" border="0" >

		<tr>
			<td >Reporte:</td>
			<td colspan="1" ><?=helpers::combo($conn, '', '-1','','','reporte','archivo','descripcion','reporte','',"SELECT A.* FROM reportes AS A INNER JOIN relacion_us_rep AS B ON A.id = B.id_reporte WHERE B.id_usuario = " . $_SESSION['id'] . " ORDER BY A.descripcion",'CargarGrid();','1')?></td>
		</tr>
		
		<tr>
			<td colspan="2"><hr /><span class="titulo">Parametros</span></td>
		</tr>
		<tr>
			<td colspan="2">
				<div id="div_parametros"></div>
			</td>
		</tr>




		<tr>
			<td colspan="2">
				<div id="gridbox" width="400" height="150" class="gridbox"></div>
			</td>
		</tr>

		<tr>
			<td align="right" colspan="3"><br /><input  type="button"  value="Generar Reporte" onClick="Imprimir()" ></td>
		</tr>
	</table>
</div>



<div style="height:40px;padding-top:10px;">
	<p id="Procesando" style="display:none;margin-top:0px;">
		<img alt="Cargando" src="images/loading.gif" /> Cargando...
	</p>
</div>

<script language="javascript"  type="text/javascript"> 
var wxR;

function Imprimir(){
var JsonAux;
var sCadena;
	sCadena = document.getElementById('reporte').value;
	mygrid.clearSelection()
	for(j=0;j<mygrid.getRowsNum();j++){
		if(j==0){
			sCadena = sCadena + '?' + mygrid.cells(mygrid.getRowId(j),1).getValue() + '=' + mygrid.cells(mygrid.getRowId(j),2).getValue();
		} else {
			sCadena = sCadena + '&' + mygrid.cells(mygrid.getRowId(j),1).getValue() + '=' + mygrid.cells(mygrid.getRowId(j),2).getValue();
		}
	}	

	if (!wxR || wxR.closed) { 
		wxR = window.open(sCadena,"winX","width=500,height=500,scrollbars=yes,resizable=yes,status=yes");
		wxR.focus()
	} else { 
		wxR.focus()
	} 

}

buildGrid();

function buildGrid(){
	//set grid parameters
	mygrid = new dhtmlXGridObject('gridbox');
	mygrid.selMultiRows = true;
	mygrid.setImagePath("js/grid/imgs/");
	mygrid.setHeader("Descripcion,Parametro,Valor");
	mygrid.setInitWidths("200,100,100");
	mygrid.setColAlign("left,left,left");
	mygrid.setColTypes("ro,ro,dhxCalendarA");
	mygrid.setColSorting("str,str,str");
	mygrid.setColumnColor("white,white,white");
	mygrid.rowsBufferOutSize = 0;
	mygrid.setMultiLine(false);
	mygrid.selmultirows="true";
	mygrid.delim=";";
    	mygrid.enableLightMouseNavigation(true);

	//start grid
	 mygrid.enableLightMouseNavigation(true); mygrid.init();
} 

function CargarGrid(){
var JsonAux;
	mygrid.clearSelection();
	mygrid.clearAll();
	JsonAux={"Reporte":$('reporte').options[$('reporte').selectedIndex].value};
	var url = 'CargarParametrosReporte.php';
	var pars = 'JsonEnv=' + JsonAux.toJSONString();
	var Request = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
	//		asynchronous:true, 
			onComplete:function(request){
				var JsonRec = eval( '(' + request.responseText + ')');
				if(JsonRec){
					var t_html = "<table class='sortable' id='grid' cellpadding='0' cellspacing='1'>";
					t_html = t_html + "<tr class='cabecera'>";
					t_html = t_html + "<td>Descripcion</td>";
					t_html = t_html + "<td>Parametro</td>";
					t_html = t_html + "<td>Valor</td>";
					t_html = t_html + "<td></td>";
					t_html = t_html + "</tr>";


					for(i=0;i<JsonRec.length;i++){
						t_html = t_html + "<tr class='filas'>";
						t_html = t_html + "<td>" + JsonRec[i]['D'] + "</td>";
						t_html = t_html + "<td>" + JsonRec[i]['P'] + "</td>";
						t_html = t_html + "<td>	<input type='text' name='parametro_" + i + "' id='parametro_" + i +"'  style='width:100px' value=''></td>";

						if(JsonRec[i]['T'] == "F") {
							t_html = t_html + "<td>";

							t_html = t_html + "<a href='#' id='fecha_parametro_" + i + "' onclick='return false;'><img border='0' src='images/calendarA.png' width='20' height='20' /></a>  ";
							t_html = t_html + "</td>";


						} else {
							t_html = t_html + "<td> </td>";
						}


						t_html = t_html + "</tr>";

						mygrid.addRow(i,JsonRec[i]['D']+";"+JsonRec[i]['P'],i);
					}
					t_html = t_html + "</table>";	

					document.getElementById("div_parametros").innerHTML=t_html;

					for (j=0;j<JsonRec.length;j++) {
						
						if(JsonRec[j]['T'] == "F") {
							var nombre_input =  "parametro_" + j;
							var nombre_boton =  "fecha_parametro_" + j;
							alert(nombre_input);
							new Zapatec.Calendar.setup({
								firstDay          : 1, 
								weekNumbers       : true,
								showOthers        : false,
								showsTime         : false,
								timeFormat        : '24', 
								step              : 2, 
								range             : [1900.01, 2999.12], 
								electric          : false, 
								singleClick       : true,
								inputField        : nombre_input ,
								button            : nombre_boton,
								ifFormat          : '%d/%m/%Y',
								daFormat          : '%Y/%m/%d',
								align             : 'Br'
							});

						}
					}

					
					/*
					for(i=0;i<JsonRec.length;i++){
						mygrid.addRow(i,JsonRec[i]['D']+";"+JsonRec[i]['P'],i);
					}

					for(i=0;i<JsonRec.length;i++) {
						if (JsonRec[i]['T'] == "F") {
							mygrid.setCellExcellType(mygrid.getRowId(i),2,"dhxCalendarA");
						} else {
							mygrid.setCellExcellType(mygrid.getRowId(i),2,"ed");
						}

					}
					*/
				}
			}
		}
	); 
}

</script>
<? require ("comun/footer.php"); ?>